package com.qingyun.form.base

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import android.widget.TextView
import androidx.annotation.IdRes
import androidx.core.view.isVisible
import androidx.core.view.updateLayoutParams
import androidx.recyclerview.widget.RecyclerView
import com.qingyun.form.R
import java.lang.ref.WeakReference
import java.util.WeakHashMap

@Suppress("UNCHECKED_CAST")
open class FormViewHolder(
    inflater: LayoutInflater,
    parent: ViewGroup,
    layoutId: Int,
) : RecyclerView.ViewHolder(inflater.inflate(layoutId, parent, false)) {
    /** Title布局的最大宽度 */
    internal var maxTitleLayoutWidth = -1

    /**
     * 缓存View，避免每次findViewById
     */
    private val viewRef = WeakHashMap<Int, WeakReference<View>>()

    /** 表单标题 Label */
    private val titleView: TextView?
        get() = getViewOrNull(R.id.tvTitle)

    /** 必填标识View */
    private val requiredView: TextView?
        get() = getViewOrNull(R.id.tvRequired)

    /** 只读表单的值 */
    private val valueTextView: TextView?
        get() = getViewOrNull(R.id.tvValue)

    /** 表单值 EditText */
    internal val valueEditText: EditText?
        get() = getViewOrNull(R.id.etValue)

    internal fun <T : View> getView(@IdRes viewId: Int): T {
        var reference = viewRef[viewId]
        if (reference?.get() == null) {
            val view = itemView.findViewById<T>(viewId)
            reference = WeakReference(view)
            viewRef[viewId] = reference
        }
        return reference.get() as T
    }

    internal fun <T : View> getViewOrNull(@IdRes viewId: Int): T? {
        var reference = viewRef[viewId]
        if (reference?.get() == null) {
            val view = itemView.findViewById<T>(viewId) ?: return null
            reference = WeakReference(view)
            viewRef[viewId] = reference
        }
        return reference.get() as T?
    }

    /**
     * 设置表单标题
     */
    internal fun setTitle(text: String) {
        titleView?.text = text
    }

    /**
     * 设置表单值
     */
    internal fun setValue(text: String?) {
        valueTextView?.text = text
        valueEditText?.setText(text)
    }

    /**
     * 设置必填标识
     */
    internal fun setRequired(required: Boolean) {
        requiredView?.visibility = if (required) View.VISIBLE else View.GONE
    }

    /**
     * 设置只读状态
     */
    internal fun setViewOnly(viewOnly: Boolean) {
        requiredView?.visibility = if (viewOnly) View.GONE else View.VISIBLE
        valueTextView?.visibility = if (viewOnly) View.VISIBLE else View.GONE
        valueEditText?.visibility = if (viewOnly) View.GONE else View.VISIBLE
    }

    fun setVisibility(id: Int, visible: Boolean) {
        getView<View>(id).isVisible = visible
    }

    fun findTitleLayout(): ViewGroup {
        return this.getView(R.id.titleLayout)
    }

    /**
     * 更新标题布局宽度
     */
    internal fun updateTitleLayout() {
        if (maxTitleLayoutWidth == -1) {
            return
        }
        val titleLayout = this.getView<ViewGroup>(R.id.titleLayout)
        if (titleLayout.layoutParams.width == maxTitleLayoutWidth) {
            return
        }
        titleLayout.updateLayoutParams {
            this.width = maxTitleLayoutWidth
        }
    }

    /**
     * 设置文本
     */
    fun setText(@IdRes id: Int, text: String?) {
        getView<TextView>(id).text = text
    }

    fun setHint(@IdRes id: Int, hintText: String?, viewOnly: Boolean = false) {
        getView<TextView>(id).hint = if (viewOnly) "" else hintText
    }

    /**
     * 清空缓存
     */
    fun clear() {
        this.viewRef.forEach {
            it.value.clear()
        }
        viewRef.clear()
    }

    internal open fun getDividerHeight(): Int {
        return -1
    }


}